package com.test.ServletUpload;

import com.test.BRBInference.BRBInference;
import com.test.jdbc.JDBCUtils2;
import com.test.readData.readData;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet(name = "UploadData_txt", value = "/UploadData_txt")
public class UploadData_txt extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, ArrayList<Double>> dataMap = null;
        String fileName = null;
        if (ServletFileUpload.isMultipartContent(request)) { //请求表单的格式
            FileItemFactory fileItemFactory = new DiskFileItemFactory();  //缓存区
            ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);

            dataMap = null;
            try {
                List<FileItem> list = servletFileUpload.parseRequest(request);  //表单列表
                dataMap = null;
                for (FileItem fileItem : list) {
                    if (fileItem.isFormField()) { //普通表
                        fileName = fileItem.getString();
                    } else {  //上传文件
                        fileItem.write(new File("F:/JAVA/apache-tomcat-7.0.104/upload/" + fileName+"_data.txt"));
                        //文件上传至指定位置
                        dataMap = readData.dataArray(new FileReader("F:/JAVA/apache-tomcat-7.0.104/upload/" + fileName+"_data.txt"));
                        //读取数据为 map
                        System.out.println(dataMap.get("x2"));
                    }
                }
                readData.storeData(fileName,dataMap);  //存数据库

            } catch (FileUploadException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        QueryRunner queryRunner = new QueryRunner(JDBCUtils2.getDataSource());
        List<Object[]> dataList = null;
        try {
            dataList = queryRunner.query("select * from "+fileName+"_data",new ArrayListHandler());
        } catch (SQLException e) {
            e.printStackTrace();
        }

        ArrayList<Object[]> dataArray = new ArrayList<>();

        for (Object[] attr : dataList) {
            Object[] Temp = new Object[attr.length];
            for (int i=0;i<attr.length;i++) {
                double temp = Double.parseDouble(attr[i].toString());
                Temp[i] = BRBInference.single_round(temp);
            }
            Temp[0] = attr[0];
            dataArray.add(Temp);
        }


        request.getSession().setAttribute("dataList_Size", dataList.size());
        request.getSession().setAttribute("dataList", dataList);
        request.getSession().setAttribute("dataList2", dataArray);

        response.sendRedirect("/Java_web/DataShow.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
